using SqlSugar;
using System;

namespace _RobotInspection
{
    public class EntityGenerator
    {
        private readonly ISqlSugarClient _db;

        public EntityGenerator(ISqlSugarClient db)
        {
            _db = db;
        }

        public void GenerateEntities(string outputPath)
        {
            _db.DbFirst.IsCreateDefaultValue().CreateClassFile(outputPath, "YourNamespace.Entities");
        }

        public void GenerateEntityByTableName(string tableName, string saveDirectory)
        {
            _db.DbFirst.Where(tableName).CreateClassFile(saveDirectory);
        }

        // 从指定数据库生成实体
        public void GenerateEntityFromSpecificDb(string configId, string tableName, string saveDirectory)
        {
            try
            {
                // 获取指定数据库连接
                var specificDb = _db.AsTenant().GetConnectionScope(configId);
                // 从指定数据库生成实体
                specificDb.DbFirst.Where(tableName).CreateClassFile(saveDirectory);
            }
            catch (Exception ex)
            {
                throw new Exception($"从数据库 '{configId}' 生成表 '{tableName}' 的实体时发生错误: {ex.Message}", ex);
            }
        }

        // 从指定数据库生成所有表的实体
        public void GenerateAllEntitiesFromSpecificDb(string configId, string saveDirectory, string nameSpace = "YourNamespace.Entities")
        {
            try
            {
                // 获取指定数据库连接
                var specificDb = _db.AsTenant().GetConnectionScope(configId);
                // 从指定数据库生成所有实体
                specificDb.DbFirst.IsCreateDefaultValue().CreateClassFile(saveDirectory, nameSpace);
            }
            catch (Exception ex)
            {
                throw new Exception($"从数据库 '{configId}' 生成所有实体时发生错误: {ex.Message}", ex);
            }
        }
    }
}
